package com.qing.picture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.picture.annotation.AuthCheck;
import com.qing.picture.common.BaseResponse;
import com.qing.picture.common.ResultUtils;
import com.qing.picture.constant.UserConstant;
import com.qing.picture.exception.ErrorCode;
import com.qing.picture.exception.ThrowUtils;
import com.qing.picture.model.dto.space.SpaceAddRequest;
import com.qing.picture.model.dto.user.*;
import com.qing.picture.model.entity.User;
import com.qing.picture.model.vo.LoginUserVo;
import com.qing.picture.model.vo.UserVo;
import com.qing.picture.service.SpaceService;
import com.qing.picture.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SpaceService spaceService;

    /**
     * 添加用户
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest request){
        ThrowUtils.throwIf(request==null, ErrorCode.NOT_FOUND_ERROR);
        User user = new User();
        BeanUtils.copyProperties(request, user);

        //初始化密码
        final String DEFAULT_PASSWORD = "123456";
        String encryptPassword = userService.getEncryptPassword(DEFAULT_PASSWORD);
        user.setUserPassword(encryptPassword);

        Boolean result = userService.save(user);
        ThrowUtils.throwIf(!result, ErrorCode.SAVE_ERROR);
            return ResultUtils.success(user.getId());
    }

    /**
     * 删除用户
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestParam("id") Long id){
        ThrowUtils.throwIf(id==null||id<=0, ErrorCode.NOT_FOUND_ERROR);
        Boolean result = userService.removeById(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新用户信息
     * @param request
     * @return
     */
    @PutMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest request){
        ThrowUtils.throwIf(request==null||request.getId()==null||request.getId()<=0, ErrorCode.NOT_FOUND_ERROR);
        User user = new User();
        BeanUtils.copyProperties(request, user);
        Boolean result = userService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUser(@RequestParam("id") Long id){
        User user = userService.getById(id);
        return ResultUtils.success(user);
    }

    /**
     * 获取脱敏后的用户信息
     * @param id
     * @return
     */
    @GetMapping("get/vo")
    public BaseResponse<UserVo> getUserVo(@RequestParam("id") Long id){
        User user = userService.getById(id);
        UserVo userVo = new UserVo();
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR, "用户不存在");
        BeanUtils.copyProperties(user, userVo);
        return ResultUtils.success(userVo);
    }

    /**
     * 获取用户列表
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVo>> getUserPageVo(@RequestBody UserQueryRequest request){
            ThrowUtils.throwIf(request==null, ErrorCode.NOT_FOUND_ERROR);
            int current = request.getCurrent();
            int size = request.getPageSize();
            Page<User> page = userService.page(new Page<>(current, size));
             userService.getQueryWrapper(request);
        Page<UserVo> userVOPage = new Page<>(current, size, page.getTotal());
        List<UserVo> userVOList = userService.getUserVOList(page.getRecords());
        userVOPage.setRecords(userVOList);
            return ResultUtils.success(userVOPage);
    }

    /**
     * 用户注册
     * @param request
     * @return
     */
    @Transactional
  @PostMapping("/register")
    public BaseResponse<Boolean> userRegister(@RequestBody @Valid UserRegisterRequest request){
//      request为空抛异常
      ThrowUtils.throwIf(request==null, ErrorCode.NOT_FOUND_ERROR);
       User user = userService.userRegister(request);
      // 5. 保存空间
      spaceService.addUserSpace(new SpaceAddRequest(),user);
        return ResultUtils.success(true);
  }
    @PostMapping("/login")
    public BaseResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        LoginUserVo loginUserVO = userService.userLogin(userLoginRequest, request);
        return ResultUtils.success(loginUserVO);
    }
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

}

